package com.kaibes.core.base.tag;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class TagMethods {
    
    private final static Map<Class<?>, TagMethod> methodMap = new HashMap<>();

    public final static TagMethod createMethod(Class<?> target) {
        TagMethod result;
        if (methodMap.containsKey(target)) {
            result = methodMap.get(target);
        } else {
            result = new TagMethod();
            Method[] methods = target.getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(MethodTag.class)) {
                    MethodTag tag = method.getAnnotation(MethodTag.class);
                    if (tag.value().isEmpty()) {
                        result.addMethod(String.valueOf(tag.intValue()), method);
                    } else {
                        result.addMethod(tag.value(), method);
                    }
                }
                
            }
            methodMap.put(target, result);
        }
        return result;
    }
    
    public static void remove(Class<?> target) {
        methodMap.remove(target);
    }
    
    public static void clear() {
        methodMap.clear();
    }
}
